package game;

import game.classes.GameAI;
import game.classes.GameMode;
import game.classes.NetworkMessage;
import game.classes.Player;
import game.classes.Pluton;
import game.classes.Ship;
import game.classes.Shot;
import game.io.Keyboard;

import game.io.PlayerScreen;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.Vector;
import java.awt.Font;
import javax.swing.JOptionPane;

/**
 * @author Chris
 */
public class Singleplayer implements GameMode {

    protected final Game game;
    protected final PlayerScreen screen;
    protected GameAI ai;
    protected Keyboard keyboard;
    protected Vector plutons;
    protected Vector shots;
    protected int TSPEED = 10;
    protected int game_paused = 0;
    protected Player player;
    protected int game_difficulty = 2000;
    protected int game_levelnum = 0;
    protected Ranking ranking;

    public Singleplayer(Game frame, Player player) {
        this.game = frame;
        this.player = player;
        this.screen = new PlayerScreen(this);
    }

    public PlayerScreen getScreen() {
        return screen;
    }

    public void play() {

        keyboard = new Keyboard(this);

        keyboard.addKey(KeyEvent.VK_SPACE);
        keyboard.addKey(KeyEvent.VK_LEFT);
        keyboard.addKey(KeyEvent.VK_RIGHT);
        keyboard.addKey(KeyEvent.VK_P);

        game.addKeyListener(keyboard);

        ai = new GameAI(this, TSPEED);

        player.setShip(new Ship((Game.BWIDTH - Ship.WIDTH) / 2, Game.BHEIGHT - Ship.HEIGHT - 2));

        newLevel();

        screen.prepare();

        ai.start();
    }

    public void notifyUpdate() {
        Graphics p;

        p = screen.getCanvas(PlayerScreen.TOP_PALET);
        p.setFont(new Font("Trebuchet MS", Font.BOLD, 25));
        p.drawString(player.getName(), 50, 35);
        p.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
        p.drawString("Puntaje: " + player.getPoints(), 40, 80);
        p.dispose();


        p = screen.getCanvas(PlayerScreen.CENTER_PALET);
        Shot current_shot;

        for (int i = 0, imax = shots.size(); i < imax; i++) {
            current_shot = (Shot) shots.get(i);
            current_shot.draw(p);
        }
        for (int i = 0, imax = plutons.size(); i < imax; i++) {
            Vector v = (Vector) plutons.get(i);
            for (int j = 0, jmax = v.size(); j < jmax; j++) {
                Pluton current_pluton = (Pluton) v.get(j);
                current_pluton.draw(p);
            }
        }

        player.drawShip(p);
        p.dispose();


        p = screen.getCanvas(PlayerScreen.BOTTOM_PALET);
        p.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
        p.drawString("Nivel: " + game_levelnum, 620, 30);
        int xoffset = 30;
        int yoffset = 15;
        int current = player.getShipEnergy();
        int e;
        for (e = 0; e < current; xoffset += 22, e++) {
            p.fillRect(xoffset, yoffset, 20, 20);
        }
        for (e = current; e < Ship.MAXENERGY; xoffset += 22, e++) {
            p.drawRect(xoffset, yoffset, 20, 20);
        }
        p.dispose();

        screen.flushBuffers();
    }

    public void notifyCollide() {
        if (!player.decShipEnergy()) {
            notifyDeath();
        }
    }

    public void notifyImpact(int reward) {
        player.addPoints(reward);
    }

    public void notifyKeypress(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_SPACE:
                player.fire(shots);
                break;
            case KeyEvent.VK_LEFT:
                player.moveShip(-1);
                break;
            case KeyEvent.VK_RIGHT:
                player.moveShip(1);
                break;
            case KeyEvent.VK_P:
                notifyPause();
                break;
        }
    }

    public void notifyKeyrelease(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_SPACE:
                break;
            case KeyEvent.VK_LEFT:
                player.moveShip(0);
                break;
            case KeyEvent.VK_RIGHT:
                player.moveShip(0);
                break;
        }
    }

    public void notifyPause() {
        if (game_paused != 2) {
            if (game_paused == 0) {
                game_paused = 1;
                ai.stop();
                screen.pauseMode();
            } else {
                game_paused = 0;
                ai.start();
            }
        }
    }

    public void notifyExit() {
        game.removeKeyListener(keyboard);
        if (new Ranking().registerScoreTo(player.getPoints(), player.getName())) {
            JOptionPane.showMessageDialog(game, "Entraste en el Ranking");
        } else {
            JOptionPane.showMessageDialog(game, "No entraste en el Ranking");
        }

        game.showMainMenu();
    }

    public void notifyWin() {
        newLevel();
    }

    public void notifyDeath() {
        ai.stop();
        screen.deathMode();
    }

    protected void newLevel() {
        if (game_difficulty - 100 > 0) {
            game_difficulty = game_difficulty - 100;
        } else if (game_difficulty - 10 > 0) {
            game_difficulty = game_difficulty - 1;
        } else {
            game_difficulty = 1;
        }
        game_levelnum++;

        shots = new Vector();
        ai.setShots(shots);
        ai.setShip(player.getShip());
        plutons = new Vector();
        ai.setPlutons(plutons);
        ai.setDifficulty(game_difficulty);
    }

    public void notifyNetworkMessage(NetworkMessage m) {
    }
}
